package com.cens.mall.commodity.service.impl;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.cens.mall.commodity.dao.BrandDao;
import com.cens.mall.commodity.dao.CategoryDao;
import com.cens.mall.commodity.entity.BrandEntity;
import com.cens.mall.commodity.entity.CategoryEntity;
import com.cens.mall.commodity.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cens.common.utils.PageUtils;
import com.cens.common.utils.Query;

import com.cens.mall.commodity.dao.CategoryBrandRelationDao;
import com.cens.mall.commodity.entity.CategoryBrandRelationEntity;
import com.cens.mall.commodity.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        BrandEntity brandEntity = brandDao.selectById(brandId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String brandName) {

    new LambdaUpdateChainWrapper<CategoryBrandRelationEntity>(this.baseMapper)
    .set(CategoryBrandRelationEntity::getBrandName,brandName)
    .eq(CategoryBrandRelationEntity::getBrandId,brandId)
    .update();

    }

    @Override
    public void updateCategory(Long categoryId, String categoryName) {
        new LambdaUpdateChainWrapper<CategoryBrandRelationEntity>(this.baseMapper)
                .set(CategoryBrandRelationEntity::getCatelogName,categoryName)
                .eq(CategoryBrandRelationEntity::getCatelogId,categoryId)
                .update();
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> list = this.baseMapper.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("catelog_id", catId)
        );
        return list.stream()
                .map(
                        item ->
                            brandService.getById(item.getBrandId())

                ).collect(Collectors.toList());
    }

}